// 120
package org.hwadee.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.hwadee.entity.*;
import org.hwadee.service.InterestService;
import org.hwadee.service.MessageService;
import org.hwadee.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/interest")
public class InterestController {

    @Autowired
    private InterestService interestService;
    @Autowired
    private PostService postService;
    @Autowired
    private MessageService messageService;

    /**
     * 增加关注关系
     * @param request
     * @param interest
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/insertInterest/{id}", method = {RequestMethod.POST})
    public Result insertInterest(HttpServletRequest request, Interest interest, HttpSession session,
                                 @PathVariable(value="id") Integer id) throws ParseException {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return new Result(403, "请先登录");
        }
        if (user.getUserId() == id) {
            return new Result(403, "你不能关注自己");
        }
        interest.setFollowerId(user.getUserId()); // 本人id
        interest.setFollowedId(id);         // 被关注者id
        try {
            interestService.insertInterest(interest);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(404);
        }
        return new Result();
    }

    /**
     * 取消关注
     * @param request
     * @param interest
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/removeInterest/{id}", method = {RequestMethod.POST})
    public Result removeInterest(HttpServletRequest request, Interest interest, HttpSession session,
                                 @PathVariable(value="id") Integer id) throws ParseException {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return new Result(403, "请先登录");
        }
        interest.setFollowerId(user.getUserId()); // 本人id
        interest.setFollowedId(id);         // 被关注者id
        try {
            interestService.deleteInterest(interest);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(404);
        }
        return new Result();
    }

    /**
     * 关注列表显示
     *
     * @param page     页码
     * @param pageSize 每页大小
     * @param model    model
     * @return 分类页面
     */
    @RequestMapping(value = "/interestFollowList")
    public String followList(@RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                             @RequestParam(value = "listStatus", defaultValue = "0") int listStatus,
                             Model model, HttpSession session) {
        model.addAttribute("listStatus",listStatus);
        User user = (User) session.getAttribute("user");
        if(listStatus!= 1) {
            PageHelper.startPage(page, pageSize);
            List<Interest> interestList = interestService.getAllFollowed(user.getUserId());
            for(Interest interest : interestList) {
                interest.setMessage(messageService.getNewestMessageByUserId(interest.getFollowerId(),interest.getFollowedId()));
            }
            PageInfo<Interest> pageInfo = new PageInfo<Interest>(interestList);
            model.addAttribute("interest", interestList);
            model.addAttribute("pageInfo", pageInfo);
        } else {
            PageHelper.startPage(page, pageSize);
            List<Interest> interestList = interestService.getAllFollower(user.getUserId());
            for(Interest interest : interestList) {
                interest.setMessage(messageService.getNewestMessageByUserId(interest.getFollowerId(),interest.getFollowedId()));
            }
            PageInfo<Interest> pageInfo = new PageInfo<Interest>(interestList);
            model.addAttribute("interest", interestList);
            model.addAttribute("pageInfo", pageInfo);
        }
        return "Admin/Interest/index";
    }

}
